Special:Log and the logging table -- unified logging scariness!
[lhc/web/wiklou.git] / includes / SpecialLog.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 function wfSpecialLog( $par = '' ) {
21 global $wgRequest;
22 $logReader =& new LogReader( $wgRequest );
23 if( '' == $wgRequest->getVal( 'type' ) && !empty( $par ) ) {
24 $logReader->limitType( $par );
25 }
26 $logViewer =& new LogViewer( $logReader );
27 $logViewer->show();
28 $logReader->cleanUp();
29 }
30
31 class LogReader {
32 var $db, $joinClauses, $whereClauses;
33 var $type = '', $user = '', $title = null;
34
35 function LogReader( $request ) {
36 $this->db =& wfGetDB( DB_SLAVE );
37 $this->setupQuery( $request );
38 }
39
40 function setupQuery( $request ) {
41 $cur = $this->db->tableName( 'cur' );
42 $user = $this->db->tableName( 'user' );
43 $this->joinClauses = array( "LEFT OUTER JOIN $cur ON log_namespace=cur_namespace AND log_title=cur_title" );
44 $this->whereClauses = array( 'user_id=log_user' );
45
46 $this->limitType( $request->getVal( 'type' ) );
47 $this->limitUser( $request->getText( 'user' ) );
48 $this->limitTitle( $request->getText( 'page' ) );
49 $this->limitTime( $request->getVal( 'from' ), '>=' );
50 $this->limitTime( $request->getVal( 'until' ), '<=' );
51
52 list( $this->limit, $this->offset ) = $request->getLimitOffset();
53 }
54
55 function limitType( $type ) {
56 if( empty( $type ) ) {
57 return false;
58 }
59 $this->type = $type;
60 $safetype = $this->db->strencode( $type );
61 $this->whereClauses[] = "log_type='$safetype'";
62 }
63
64 function limitUser( $name ) {
65 $title = Title::makeTitleSafe( NS_USER, $name );
66 if( empty( $name ) || is_null( $title ) ) {
67 return false;
68 }
69 $this->user = str_replace( '_', ' ', $title->getDBkey() );
70 $safename = $this->db->strencode( $this->user );
71 $user = $this->db->tableName( 'user' );
72 $this->whereClauses[] = "user_name='$safename'";
73 }
74
75 function limitTitle( $page ) {
76 $title = Title::newFromText( $page );
77 if( empty( $page ) || is_null( $title ) ) {
78 return false;
79 }
80 $this->title =& $title;
81 $safetitle = $this->db->strencode( $title->getDBkey() );
82 $ns = $title->getNamespace();
83 $this->whereClauses[] = "log_namespace=$ns AND log_title='$safetitle'";
84 }
85
86 function limitTime( $time, $direction ) {
87 # Direction should be a comparison operator
88 if( empty( $time ) ) {
89 return false;
90 }
91 $safetime = $this->db->strencode( wfTimestamp( TS_MW, $time ) );
92 $this->whereClauses[] = "log_timestamp $direction '$safetime'";
93 }
94
95 function getQuery() {
96 $logging = $this->db->tableName( "logging" );
97 $user = $this->db->tableName( 'user' );
98 $sql = "SELECT log_type, log_action, log_timestamp,
99 log_user, user_name,
100 log_namespace, log_title, cur_id,
101 log_comment FROM $logging, $user ";
102 if( !empty( $this->joinClauses ) ) {
103 $sql .= implode( ',', $this->joinClauses );
104 }
105 if( !empty( $this->whereClauses ) ) {
106 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses );
107 }
108 $sql .= " ORDER BY log_timestamp DESC ";
109 $sql .= $this->db->limitResult( $this->limit, $this->offset );
110 return $sql;
111 }
112
113 function initQuery() {
114 $this->result = $this->db->query( $this->getQuery() );
115 }
116
117 function fetchObject() {
118 return $this->db->fetchObject( $this->result );
119 }
120
121 function cleanUp() {
122 $this->db->freeResult( $this->result );
123 }
124
125 function queryType() {
126 return $this->type;
127 }
128
129 function queryUser() {
130 return $this->user;
131 }
132
133 function queryTitle() {
134 if( is_null( $this->title ) ) {
135 return '';
136 } else {
137 return $this->title->getPrefixedText();
138 }
139 }
140 }
141
142 class LogViewer {
143 var $reader, $skin;
144 var $typeText = array(
145 '' => array( 'log', 'alllogstext' ),
146 'block' => array( 'blocklogpage', 'blocklogtext' ),
147 'protect' => array( 'protectlogpage', 'protectlogtext' ),
148 'rights' => array( 'bureaucratlog', '' ),
149 'delete' => array( 'dellogpage', 'dellogpagetext' ),
150 'upload' => array( 'uploadlogpage', 'uploadlogpagetext' )
151 );
152
153
154 function LogViewer( &$reader ) {
155 global $wgUser;
156 $this->skin =& $wgUser->getSkin();
157 $this->reader =& $reader;
158 }
159
160 function show() {
161 global $wgOut;
162 $this->showHeader( $wgOut );
163 $this->showOptions( $wgOut );
164 $this->showPrevNext( $wgOut );
165 $out = "";
166 $this->reader->initQuery();
167 while( $s = $this->reader->fetchObject() ) {
168 $out .= $this->logLine( $s );
169 }
170 $wgOut->addHTML( $out );
171 $this->showPrevNext( $wgOut );
172 }
173
174 # wfMsg( unprotectedarticle, $text )
175 # wfMsg( 'protectedarticle', $text )
176 # wfMsg( 'deletedarticle', $text )
177 # wfMsg( 'uploadedimage', $text )
178 # wfMsg( "blocklogentry", $this->BlockAddress, $this->BlockExpiry );
179 # wfMsg( "bureaucratlogentry", $this->mUser, implode( " ", $rightsNotation ) );
180 # wfMsg( "undeletedarticle", $this->mTarget ),
181 function logLine( $s ) {
182 global $wgLang;
183 $title = Title::makeTitle( $s->log_namespace, $s->log_title );
184 $user = Title::makeTitleSafe( NS_USER, $s->user_name );
185 $time = $wgLang->timeanddate( $s->log_timestamp );
186 if( $s->cur_id ) {
187 $titleLink = $this->skin->makeKnownLinkObj( $title );
188 } else {
189 $titleLink = $this->skin->makeBrokenLinkObj( $title );
190 }
191 $userLink = $this->skin->makeLinkObj( $user, htmlspecialchars( $s->user_name ) );
192 if( '' === $s->log_comment ) {
193 $comment = '';
194 } else {
195 $comment = '(<em>' . $this->skin->formatComment( $s->log_comment ) . '</em>)';
196 }
197
198 $action = LogPage::actionText( $s->log_type, $s->log_action, $titleLink );
199 $out = "<li>$time $userLink $action $comment</li>\n";
200 return $out;
201 }
202
203 function showHeader( &$out ) {
204 $type = $this->reader->queryType();
205 if( isset( $this->typeText[$type] ) ) {
206 list( $title, $headertext ) = $this->typeText[$type];
207 $out->setPageTitle( str_replace( '_', ' ', wfMsg( $title ) ) );
208 $out->addWikiText( wfMsg( $headertext ) );
209 }
210 }
211
212 function showOptions( &$out ) {
213 global $wgScript;
214 $action = htmlspecialchars( $wgScript );
215 $title = Title::makeTitle( NS_SPECIAL, 'Log' );
216 $special = htmlspecialchars( $title->getPrefixedDBkey() );
217 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
218 "<input type='hidden' name='title' value=\"$special\" />\n" .
219 $this->getTypeMenu() .
220 $this->getUserInput() .
221 $this->getTitleInput() .
222 "<input type='submit' />" .
223 "</form>" );
224 }
225
226 function getTypeMenu() {
227 $out = "<select name='type'>\n";
228 foreach( $this->typeText as $type => $msg ) {
229 $text = htmlspecialchars( str_replace( '_', ' ', wfMsg( $msg[0] ) ) );
230 $selected = ($type == $this->reader->queryType()) ? ' selected="selected"' : '';
231 $out .= "<option value=\"$type\"$selected>$text</option>\n";
232 }
233 $out .= "</select>\n";
234 return $out;
235 }
236
237 function getUserInput() {
238 $user = htmlspecialchars( $this->reader->queryUser() );
239 return "User: <input type='text' name='user' size='12' value=\"$user\" />\n";
240 }
241
242 function getTitleInput() {
243 $title = htmlspecialchars( $this->reader->queryTitle() );
244 return "Title: <input type='text' name='page' size='20' value=\"$title\" />\n";
245 }
246
247 function showPrevNext( &$out ) {
248 global $wgLang;
249 $pieces = array();
250 $pieces[] = 'type=' . htmlspecialchars( $this->reader->queryType() );
251 $pieces[] = 'user=' . htmlspecialchars( $this->reader->queryUser() );
252 $pieces[] = 'page=' . htmlspecialchars( $this->reader->queryTitle() );
253 $bits = implode( '&', $pieces );
254 $offset = 0; $limit = 50;
255
256 # TODO: use timestamps instead of offsets to make it more natural
257 # to go huge distances in time
258 $html = wfViewPrevNext( $offset, $limit,
259 $wgLang->specialpage( 'Log' ),
260 $bits,
261 false);
262 $out->addHTML( '<p>' . $html . '</p>' );
263 }
264 }
265
266
267 ?>